package com.haoniu.common.service.impl;

import com.haoniu.common.service.AliOssService;
import com.haoniu.common.service.QiNiuService;
import com.haoniu.common.service.FileUploadService;
import com.haoniu.common.utils.DateUtils;
import com.haoniu.common.utils.UuidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author shilvfei
 */
@Service("fileUpload")
public class FileUploadServiceImpl implements FileUploadService {

    /**
     *  文件服务器
     */
    @Value("${file.upload.server}")
    private String uploadServer;

    @Autowired
    private QiNiuService qiNiuService;

    @Autowired
    private AliOssService aliOssService;

    /**
     * 获取七牛云地址
     * @return
     */
    @Override
    public String getFileBaseUrl(){
        if(uploadServer.equals("ali")){
            return aliOssService.getFileBaseUrl();
        }else {
            //qi niu
            return qiNiuService.getFileBaseUrl();
        }
    }

    /**
     * 生成上传授权uptoken
     * */
    @Override
    public String getToken() {
        if(uploadServer.equals("ali")){
            return "";
        }else {
            //qiniu
            return qiNiuService.getToken();
        }
    }

    /**
     * @param uploadFile 上传的文件
     * @param type       上传的图片类型 如：banner 。user
     * @return
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile uploadFile, String type) throws Exception {
        String fileName = uploadFile.getOriginalFilename();
        String formatName = getFormatName(fileName);
        String key = generalDir(type) + "/" + DateUtils.datePath() + "/" + UuidUtils.uuid() + "." + formatName;
        if(uploadServer.equals("ali")){
            return aliOssService.uploadFile(uploadFile,key);
        }else {
            //qiniu
            return qiNiuService.uploadFile(uploadFile,key);
        }
    }

    /**
     * 图片所属分类
     * @param type
     * @return
     */
    public static String generalDir(String type) {
        String dir = "default";
        if(StringUtils.isNotBlank(type)){
            dir=type;
        }
        return dir;
    }

    /**
     * 获取文件文件格式
     *
     * @param name
     *            图片文件
     * @return 图片文件格式
     */
    public static String getFormatName(String name) {
        if (StringUtils.isBlank(name)) {
            return "jpg";
        }
        try {
            String formatName = null;
            if (name == null) {
                return "jpg";
            }
            int l = name.lastIndexOf(".");
            if (l <= 0 || l + 1 >= name.length()) {
                return "jpg";
            }
            formatName = name.substring(l + 1, name.length());
            formatName = formatName.toLowerCase();

            return formatName;
        } catch (Exception e) {
            e.printStackTrace();
            return "jpg";
        }
    }

}
